package com.dgut.medicalsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.medicalsystem.entity.Bill;
import com.dgut.medicalsystem.entity.Information;
import com.dgut.medicalsystem.mapper.AppointmentMapper;
import com.dgut.medicalsystem.mapper.BillMapper;
import com.dgut.medicalsystem.mapper.DepartmentMapper;
import com.dgut.medicalsystem.mapper.InformationMapper;
import com.dgut.medicalsystem.service.AppointmentService;
import com.dgut.medicalsystem.service.InformationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dgut.medicalsystem.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 坚宝医疗
 * @since 2021-09-03
 */
@Service
public class InformationServiceImpl extends ServiceImpl<InformationMapper, Information> implements InformationService {

    @Autowired
    InformationMapper informationMapper;

    @Autowired
    AppointmentMapper appointmentMapper;

    @Autowired
    BillMapper billMapper;

    @Override
    public boolean addInformation(Information information) {
        information.setTime(LocalDateTime.now());
        return this.save(information);
    }

    @Override
    public IPage<Information> searchInformation(Integer page, Integer size) {
        Page<Information> informationPage = new Page<>(page,size);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.orderByDesc("time");
        return informationMapper.selectPage(informationPage,wrapper);
    }

    @Override
    public boolean updateInformation(Information information) {
        information.setTime(LocalDateTime.now());
        return this.updateById(information);
    }

    @Override
    public boolean deleteInformation(Integer id) {
        return this.removeById(id);
    }

    @Override
    public Map<String, Object> getAppDetails(LocalDate date) {
        Map<String, Object> info = new HashMap<>();
        LocalDate endDate = date.plusDays(1);

        QueryWrapper countWrapper = new QueryWrapper();
        countWrapper.between("appointment_date",date,endDate);
        Integer count = appointmentMapper.selectCount(countWrapper);
        if (count != 0) {
            info.put("appointmentCount",count);
            List<Map<String, Object>> appInfo = appointmentMapper.getAppointmentDetails(date,endDate);
            info.put("appInfo",appInfo);
        } else {
            return null;
        }
        return info;
    }

    @Override
    public Map<String, Object> getBillDetails(LocalDate date) {
        Map<String, Object> info = new HashMap<>();
        LocalDate endDate = date.plusDays(1);

        DecimalFormat df = new DecimalFormat("#.00");
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.select("sum(price) as price,sum(appo_price) as appo_price").between("bill_date",date,endDate);
        Bill bill = billMapper.selectOne(wrapper);
        if (bill == null) {
            info.put("msg","暂无流水信息");
        } else {
            Double sum = bill.getPrice();
            Double appo_sum = bill.getAppoPrice();
            info.put("sum",df.format(sum));
            info.put("appo_sum",df.format(appo_sum));
            List<Map<String, Object>> billInfo = appointmentMapper.getBillDetails(date,endDate);
            for (Map<String, Object> map:billInfo) {
                sum = (Double) map.get("price");
                appo_sum = (Double) map.get("appo_price");
                map.put("price",df.format(sum));
                map.put("appo_price",df.format(appo_sum));
            }
            info.put("billInfo",billInfo);
            info.put("msg","流水信息获取成功");
        }

        return info;
    }

    @Override
    public List<Information> getAllInfos() {
        return informationMapper.getAllInfos();
    }

    @Override
    public Map<String, Object> daysAppInfo(Integer days) {
        Map<String, Object> info = new HashMap<>();
        LocalDate now = LocalDate.now();
        List<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i<days; i++) {
            LocalDate local = now.minusDays(i);
            QueryWrapper countWrapper = new QueryWrapper();
            countWrapper.between("appointment_date",local,local.plusDays(1));
            Integer count = appointmentMapper.selectCount(countWrapper);
            Map<String, Object> appInfo = new HashMap<>();
            appInfo.put("date", local);
            appInfo.put("count",count);
            list.add(appInfo);
        }
        info.put("list",list);
        info.put("msg","信息获取成功");
        return info;
    }

    @Override
    public Map<String, Object> daysBillInfo(Integer days) {
        Map<String, Object> info = new HashMap<>();
        LocalDate now = LocalDate.now();
        List<Map<String, Object>> list = new ArrayList<>();

        DecimalFormat df = new DecimalFormat("#.00");
        for (int i=0; i<days; i++) {
            LocalDate local = now.minusDays(i);
            Map<String, Object> billInfo = new HashMap<>();
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.select("sum(price) as price,sum(appo_price) as appo_price").between("bill_date",local,local.plusDays(1));
            Bill bill = billMapper.selectOne(wrapper);
            if (bill != null) {
                Double sum = bill.getPrice();
                Double appo_sum = bill.getAppoPrice();
                billInfo.put("sum",df.format(sum));
                billInfo.put("appo_sum",df.format(appo_sum));
            } else {
                billInfo.put("sum","0.00");
                billInfo.put("appo_sum","0.00");
            }
            billInfo.put("date",local);
            list.add(billInfo);
        }
        info.put("list",list);
        info.put("msg","信息获取成功");
        return info;
    }
}
